#include <iostream>
#include <cstdio>
#include <jpeglib.h>

using namespace std;

int main(int argc, char *argv[])
{
    struct jpeg_decompress_struct cinfo;
    struct jpeg_error_mgr jerr;
    
    // More stuff
    // Source file
    FILE *inFile;
    if((inFile = fopen("D:\\downloads\\002\\002\\MG00N037.JPG", "rb")) == NULL) {
        return 0;
    }

    cinfo.err = jpeg_std_error(&jerr);
    //jerr.error_exit = JPEGError;
    jpeg_create_decompress(&cinfo);
    jpeg_stdio_src(&cinfo, inFile);

    (void) jpeg_read_header(&cinfo, TRUE);
    (void) jpeg_start_decompress(&cinfo);
    if (cinfo.output_components == 1) { // Grayscale image
        //mergeGrayJpegFile(cinfo, output, directoryNumber, directoryCount);
    } else if (cinfo.output_components == 3) { // Color image

            int row_stride = cinfo.output_width * cinfo.output_components;
            unsigned char *colorJpegPic;
            //JSAMPROW *colorJpegPic;
            int width = cinfo.output_width;
            int height = cinfo.output_height;
            int components = cinfo.output_components;
            // Allocation for a Color picture
            colorJpegPic = new unsigned char[width * height * components];
            //colorJpegPic = (*cinfo.mem->alloc_sarray) ((j_common_ptr) &cinfo, JPOOL_IMAGE, row_stride, cinfo.output_height);        

            printf(("Width: %d\nHeight: %d\nComponents: %d\n"), cinfo.output_width, cinfo.output_height, cinfo.output_components);

            while (cinfo.output_scanline < cinfo.output_height) {                      // read a jpeg picture file 
               //printf(("Scanline: %d\n"), cinfo.output_scanline);
                //unsigned char* copy = colorJpegPic;
                unsigned char* scanline = colorJpegPic + cinfo.output_scanline * row_stride;
                (void) jpeg_read_scanlines(&cinfo, &scanline, 1);
                //(void) jpeg_read_scanlines(&cinfo, &colorJpegPic[cinfo.output_scanline], 1);
               
               for (int i = 0; i < width / 5; i++) {
                    for (int k = 0; k < components; k++) {
                        printf("%d ", colorJpegPic[(cinfo.output_scanline - 1) * row_stride + i + k]);
                    }
                    printf("\t");
                }
                printf("\n");
                //colorJpegPic = copy;
               
            }

            
            

            //TIFFSetField(output, TIFFTAG_PAGENUMBER, directoryNumber++, directoryCount);
            //
            //TIFFSetField(output, TIFFTAG_IMAGEWIDTH, width);             // Set fields of a TIFF file  
            //TIFFSetField(output, TIFFTAG_IMAGELENGTH, height);

            //TIFFSetField(output, TIFFTAG_COMPRESSION, COMPRESSION_DEFLATE);
            //TIFFSetField(output, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);          // set storage organization to 1
            //TIFFSetField(output, TIFFTAG_PHOTOMETRIC,PHOTOMETRIC_RGB);
            //TIFFSetField(output, TIFFTAG_BITSPERSAMPLE, 8);                           // 8 bit pier pixel     
            //TIFFSetField(output, TIFFTAG_SAMPLESPERPIXEL, 3);                          // 1 channel/sample/component per pixel 
           
            //TIFFSetField(output, TIFFTAG_XRESOLUTION, cinfo.X_density);
            //TIFFSetField(output, TIFFTAG_YRESOLUTION, cinfo.Y_density);
            //TIFFSetField(output, TIFFTAG_RESOLUTIONUNIT, cinfo.density_unit);

            //TIFFWriteEncodedStrip(output, 0, colorJpegPic, width * height * components);
            //for(unsigned int i = 0; i< height; i++)
            //    TIFFWriteEncodedStrip(output, i, colorJpegPic[i], width * components); 
            //TIFFWriteEncodedStrip(output, 0, colorJpegPic[0], width * components);
	        //TIFFWriteDirectory(output);   
            
            (void) jpeg_finish_decompress(&cinfo);
            jpeg_destroy_decompress(&cinfo);
            delete[] colorJpegPic;
    }

    fclose(inFile);
    return 0;
}